﻿<script>

    var _vm;
    $(function () {
        var vm = new ViewModel();
        _vm = vm;
        ko.applyBindings(vm);
        vm.Init();
    });

    function ViewModel() {
        var me = this;
        ViewModelBase.call(me);
        vmExtend.call(me);//读取所有数据

        me.DeleteUrl = "@this.ContentUrl("~/SystemManage/User/Delete")";
        //me.DataTable = new DataTableBase(me);
        me.Dialog = new Dialog(me);
        ////打开重置密码对话框
        me.RevisePasswordDialog = new RevisePasswordDialog();
        me.OpenRevisePasswordDialog = function () {
            me.RevisePasswordDialog.Open(me.DataTable.SelectedModel());
        }
        //详情页
        me.DetailDialog = new DetailDialog(me);
        me.Detail = function () {
            me.DetailDialog.Open(me.DataTable.SelectedModel(), "详情");
        }
    }
    function vmExtend() {
        var me = this;

        me.Init = function () {
            me.LoadModels();
        }

        me.LoadModels = function (page) {
            me.DataTable.SelectedModel(null);
            var params = me.SearchModel();
            params.page = page || 1;
            $ace.get("@this.ContentUrl("~/SystemManage/User/GetModels")", params, function (result) {
                me.DataTable.SetPagedData(result.Data);
            }
          );
        }
    }

    function Dialog(vm) {
        var me = this;
        DialogBase.call(me);


        me.OnOpen = function () {
            var model = me.EditModel();
            if (model) {
                var bindModel = $ko.toJS(model);
                me.Model(bindModel);
            }
            else {
                me.Model({ IsEnabled: true });
            }

            initControl();
            //选中的用户ID
            var userId = me.Model().id;
            //角色树的加载
            $("#permissionTree").html(null);
            $("#permissionTree").treeview({
                height: 256,
                showcheck: true,
                url: "/SystemManage/UserAuthorize/GetPermissionTree",
                //url: "/SystemManage/User/GetPermissionTree",
                param: { userId: userId },
                @*cbiconpath: "@this.ContentUrl("/Content/wdtree/images/icons/")"*@
            });
            //组树的加载
            $("#permissionGroupTree").html(null);
            $("#permissionGroupTree").treeview({
                height: 256,
                showcheck: true,
                url: "/SystemManage/UserAuthorize/GetPermissionGroupTree",
                //url: "/SystemManage/User/GetPermissionGroupTree",
                param: { userId: userId },
                cbiconpath: "@this.ContentUrl("/Content/js/wdtree/images/icons/")"
            });
        }
        //点击保存
        me.OnSave = function () {
            //实体
            var model = me.Model();

            if (!$('#form1').formValid()) {
                return false;
            }
            //角色树的权限
            var permissionIdArr = $("#permissionTree").getCheckedNodes();
            var permissionIds = "";
            var c = "";
            for (var i = 0; i < permissionIdArr.length; i++) {
                permissionIds += c + permissionIdArr[i];
                c = ",";
            }
            //组树的权限
            var permissionIdGroupArr = $("#permissionGroupTree").getCheckedNodes();
            var permissionGroupIds = "";
            var c = "";
            for (var i = 0; i < permissionIdGroupArr.length; i++) {
                permissionGroupIds += c + permissionIdGroupArr[i];
                c = ",";
            }
            //角色树的权限
            model.permissionIds = permissionIds;
            model.permissionGroupIds = permissionGroupIds;
            //修改的保存
            if (me.EditModel()) {
                $ace.post("@this.ContentUrl("~/SystemManage/User/Edit")", model, function (result) {
                    $ace.msg(result.Msg);
                    me.Close();
                    vm.LoadModels();
                }
               );
            }
            else {
                $ace.post("@this.ContentUrl("~/SystemManage/User/Add")", model, function (result) {
                    $ace.msg(result.Msg);
                    me.Close();
                    vm.LoadModels();
                }
             );
            }
        }
        //控制上一步，下一步，按钮的显隐性
        function initControl() {
            var w = $('#wizard').wizard();
            w.on('change', function (e, data) {
                var $finish = $("#btn_finish");
                var $next = $("#btn_next");
                if (data.direction == "next") {
                    switch (data.step) {
                        case 2://第二步之后将完成按钮显示，下一步隐藏
                            if (!$('#form1').formValid()) {
                                return false;
                            }
                            $finish.show();
                            $next.hide();
                            break;
                        default:
                            break;
                    }
                }
                else {
                    $finish.hide();
                    $next.show();
                }
            });
        }
    }

    //详情页对话框设置
    function DetailDialog() {
        var me = this;
        DialogBase.call(me);

        me.OnOpen = function () {
            //将“保存”按钮隐藏
            me.ShowSaveButton(false);
            var model = me.EditModel();
            if (model) {
                var bindModel = $ko.toJS(model);
                me.Model(bindModel);
            }
            else {
                me.Model({});
            }
        }
    }
    //重置密码
    function RevisePasswordDialog(vm) {
        var me = this;
        DialogBase.call(me);
        me.Title("重置密码");

        me.OnOpen = function () {
            var model = me.EditModel();
            if (!model) {
                throw new Error();
            }

            var bindModel = $ko.toJS(model);
            var m = {};
            m.id = bindModel.id;
            m.username = bindModel.username;
            m.password = bindModel.password;
            //控件的name属性值=绑定的字段名字
            //  m.RealName = bindModel.RealName;

            me.Model(m);
        }
        me.OnSave = function () {
            if (!$('#RevisePasswordForm').formValid()) {
                return false;
            }

            var model = me.Model();
            var pwd = model.password;
            var data = { userId: model.id, newPassword: pwd };
            $ace.post("@this.ContentUrl("~/SystemManage/User/OpenRevisePasswordDialog")", data, function (result) {
                $ace.msg(result.Msg);
                me.Close();
                //vm.LoadModels();
            }
           );
        }
    }
</script>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           